package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuang.mapper.AstControllerHeartMapper;
import com.kuang.mapper.AstControllerPoweronMapper;
import com.kuang.mapper.AstManageDeviceOpenMapper;
import com.kuang.mapper.pfsi.AstPfsiDeviceOpenMapper;
import com.kuang.pojo.AstControllerHeart;
import com.kuang.pojo.AstControllerPoweron;
import com.kuang.pojo.AstManageDeviceOpen;
import com.kuang.pojo.pfsi.AstPfsiDeviceOpen;
import com.kuang.utils.ControllerLogUtils;
import com.kuang.utils.MQTTConnect;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/asset/")
public class ImportGetDeviceMsgController {

    Logger logger = Logger.getLogger(ImportGetDeviceMsgController.class);

    @Resource
    AstControllerPoweronMapper astControllerPoweronMapper;
    @Resource
    AstManageDeviceOpenMapper astManageDeviceOpenMapper;
    @Resource
    AstPfsiDeviceOpenMapper astPfsiDeviceOpenMapper;

    @RequestMapping("getDeviceMsg")
    @ResponseBody
    public String ImportRemoteCloseWell(@RequestBody String json) {
        logger.info("接收："+json);
        JSONObject jsonObject = JSON.parseObject(json);
        String controller_name="获取设备信息";
        //获取json数据
        String device_code = jsonObject.getString("device_code");
        String device_type = jsonObject.getString("device_type");

        Map check = check(jsonObject);

        if (check != null) {
            //结果集
            Map errorResult = new LinkedHashMap();
            String string = MapUtils.getString(check, "500");
            errorResult.put("success", "false");
            errorResult.put("code", "500");
            errorResult.put("msg", string);
            JSONObject data = new JSONObject();
            data.put("device_code",device_code);
            data.put("device_type",device_type);
            data.put("zone_code","");
            data.put("longitude","");
            data.put("latitude","");
            errorResult.put("data",data);
            int i = ControllerLogUtils.setErrorMsgLog("import", "/asset/getDeviceMsg",
                    controller_name,string, json, JSON.toJSONString(errorResult));
            logger.info("返回:"+JSON.toJSONString(errorResult));
            return JSON.toJSONString(errorResult);
        }

        Map result = selectOpen(device_code, device_type);
        String success = (String) result.get("success");
        String msg = (String) result.get("msg");

        if (StringUtils.equals(success,"true")){
            int i = ControllerLogUtils.setMsgLog("import", "/asset/getDeviceMsg",
                    controller_name,msg, json, JSON.toJSONString(result));
        }else {
            int i = ControllerLogUtils.setErrorMsgLog("import", "/asset/getDeviceMsg",
                    controller_name,msg, json, JSON.toJSONString(result));
        }

        logger.info("返回:"+JSON.toJSONString(result));
        return JSON.toJSONString(result);
    }

    /**
     * 校验
     * @param jsonMap
     * @return
     */
    public Map check(JSONObject jsonMap) {
        Map error = new LinkedHashMap();
        if (StringUtils.isBlank(MapUtils.getString(jsonMap, "device_code", ""))) {
            error.put("500", "设备编码未填写");
            return error;
        }
        String device_type = MapUtils.getString(jsonMap, "device_type", "");
        if (StringUtils.isBlank(device_type)) {
            error.put("500", "设备类型未填写");
            return error;
        }
        if (!StringUtils.contains("controller,manage,pfsi",device_type)){
            error.put("500", "设备类型校验【"+device_type+"】未通过请检查【controller,manage,pfsi】");
            return error;
        }

        return null;
    }

    /**
     * 查询
     * @param device_code
     * @param device_type
     * @return
     */
    public Map  selectOpen(String device_code,String device_type){
        Map result = new LinkedHashMap();
        result.put("success", "true");
        result.put("code", "200");
        result.put("msg", "处理成功！");

        Map errorResult = new LinkedHashMap();
        errorResult.put("success", "false");
        errorResult.put("code", "500");
        errorResult.put("msg", "未找到");

        JSONObject data = new JSONObject();
        data.put("device_code",device_code);
        data.put("device_type",device_type);

        if (StringUtils.equals("controller",device_type)){
            AstControllerPoweron astControllerPoweron = new AstControllerPoweron();
            astControllerPoweron.setDevice_code(device_code);
            List<AstControllerPoweron> astControllerPowerons = astControllerPoweronMapper.selectAllByExample(astControllerPoweron);
            if (astControllerPowerons.size()==0){
                data.put("zone_code","");
                data.put("longitude","");
                data.put("latitude","");
                errorResult.put("data",data);
                return  errorResult;
            }
            data.put("zone_code",astControllerPowerons.get(0).getZone_code());
            data.put("longitude",astControllerPowerons.get(0).getLongitude());
            data.put("latitude",astControllerPowerons.get(0).getLatitude());

        }else if (StringUtils.equals("manage",device_type)){
            AstManageDeviceOpen astManageDeviceOpen = new AstManageDeviceOpen();
            astManageDeviceOpen.setDevice_code(device_code);
            List<AstManageDeviceOpen> astManageDeviceOpens = astManageDeviceOpenMapper.selectAllByExample(astManageDeviceOpen);
            if (astManageDeviceOpens.size()==0){
                data.put("zone_code","");
                data.put("longitude","");
                data.put("latitude","");
                errorResult.put("data",data);
                return  errorResult;
            }
            data.put("zone_code",astManageDeviceOpens.get(0).getZone_code());
            data.put("longitude",astManageDeviceOpens.get(0).getLongitude());
            data.put("latitude",astManageDeviceOpens.get(0).getLatitude());

        }else if (StringUtils.equals("pfsi",device_type)){
            AstPfsiDeviceOpen astPfsiDeviceOpen = new AstPfsiDeviceOpen();
            astPfsiDeviceOpen.setDevice_code(device_code);
            List<AstPfsiDeviceOpen> astPfsiDeviceOpens = astPfsiDeviceOpenMapper.selectAllByExample(astPfsiDeviceOpen);
            if (astPfsiDeviceOpens.size()==0){
                data.put("zone_code","");
                data.put("longitude","");
                data.put("latitude","");
                errorResult.put("data",data);
                return  errorResult;
            }
            data.put("zone_code","");
            data.put("longitude",astPfsiDeviceOpens.get(0).getLongitude());
            data.put("latitude",astPfsiDeviceOpens.get(0).getLatitude());

        }

        result.put("data", data);
        return result;
    }

}
